package com.google.code.jholidays.events;

import com.google.code.jholidays.core.EventDescriptor;

/**
 * Provides skeletal implementation of {@link IFactory} interface.
 * <p>
 * Uses template method pattern in order to create events. Subclass must
 * override two methods:
 * 
 * <pre>
 * 1. {@link #matches(EventDescriptor)}
 * 2. {@link #analizeExpression(EventDescriptor, IEvent)}
 * </pre>
 * 
 * <code>AbstractFactory</code> builds {@link AbstractEvent} subclasses. When
 * <code>createEvent()</code> is called factory checks whether it can actually
 * produce event using given descriptor. Then it calls <code>matches</code>
 * method in order to actually produce event.
 * 
 * @author tillias
 * 
 */
public abstract class AbstractFactory implements IFactory {

    /**
     * {@inheritDoc}
     * <p>
     * This implementation uses <i>template method</i> pattern for creating
     * events. Override <tt>analizeExpression()</tt> and <tt>matches()</tt>
     * methods in subclasses in order to provide concrete implementations of
     * abstract factory
     */
    @Override
    public IEvent createEvent(EventDescriptor descriptor, IEvent parent) {
	if (matches(descriptor)) {
	    return analizeExpression(descriptor, parent);
	}

	return null;
    }

    @Override
    public abstract boolean matches(EventDescriptor descriptor);

    /**
     * Parses expression of given event descriptor and produces event.
     * <p>
     * This method is designed for overriding in subclasses.
     * 
     * @param descriptor
     *            Event descriptor
     * @param parent
     *            Parent event
     * @return Constructed event
     */
    protected abstract AbstractEvent analizeExpression(
	    EventDescriptor descriptor, IEvent parent);

}
